package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.user.DeleteUserDeptBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.UserDeptDao;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.EnumUtil;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.UserDeptDeleteType;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;

/**
 * 解除部门资源关系 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/11 12:20
 */
@Component("userDeptDelete")
public class UserDeptDelete extends AbstractProcessor<Object, DeleteUserDeptBo> implements Processor<Object, DeleteUserDeptBo> {

    @Autowired
    private UserDeptDao userDeptDao;
    
    
    /**
     * 校验
     */
    @Override
    public void check(DeleteUserDeptBo bo) throws AabException {
    	
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteUserDeptBo bo) {
    	Integer type = bo.getType();
    	if (UserDeptDeleteType.根据用户删除用户部门关系.sameKey(type)) {
    		Long userId = bo.getUserId();
    		List<Long> deptIdList = bo.getDeptIdList();
    		userDeptDao.deleteByUserDept(bo, userId, deptIdList);
    	} else if (UserDeptDeleteType.根据部门删除用户部门关系.sameKey(type)) {
    		Long deptId = bo.getDeptId();
    		List<Long> userIdList = bo.getUserIdList();
    		userDeptDao.deleteByDeptUser(bo, deptId, userIdList);
    	}
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteUserDeptBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.用户部门关系解除.key());
    	record.setTargetId(param.getType().longValue());
    	record.setTargetName(EnumUtil.containsKey(UserDeptDeleteType.class, param.getType()));
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
		return null;
	}
}
